<?php
	class Giathuetheongay_model extends CI_Model 
	{
	    function LayGiaThue_CuaMotPhieuDat($mapd) 
		{
        	$this->db->select('*');
	        $this->db->from('GiaThueTheoNgay');
    	    $this->db->where('PhieuDatPhong', $mapd);
        	$this->db->order_by('Ngay', 'asc');
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function Xoa($magt) 
		{
        	return $this->db->delete('GiaThueTheoNgay', array('MaGT' => $magt));
	    }

    	function add_records_giathuetheongay($startdate, $enddate, $id, $loaiphong) 
		{
        	date_add($enddate, date_interval_create_from_date_string('1 days'));
	        do 
			{
    	        $start = date_format($startdate, 'Y-m-d');
        	    $sql = 'SELECT GiaSauKhuyenMai FROM PhongTrongTheoNgay WHERE LoaiPhong = ? and Ngay = ? LIMIT 0,1';
            	$q = $this->db->query($sql, array($loaiphong, $start));
	            if ($q->num_rows() > 0) 
				{
                	foreach ($q->result() as $row) 
					{
                    	$gia = $row->GiaSauKhuyenMai;
	                }
    	        }
        	    $data = array(
				                'Ngay' => $start,
                				'Gia' => $gia,
				                'PhieuDatPhong' => $id
							);
	            date_add($startdate, date_interval_create_from_date_string('1 days'));
    	        $this->db->insert('GiaThueTheoNgay', $data);
        	} while ($startdate != $enddate);
	        return;
    	}
	}
?>